package com.dat255.tentatime;

import java.util.ArrayList;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

/**
 * This activity will only display a list of the results
 * from a performed search
 * @author Joseph
 *
 */
public class ResultActivity extends ListActivity {
	private TextView title;
	 /** 
     * Called when the activity is first created. 
     * This method will also add all the exams from the previous activity
     * and display it on the listview
     */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		//Set the layout
		setContentView(R.layout.result);
		
		//Set the title
		title = (TextView) findViewById(R.id.listName);
		title.setText(R.string.titleResult);
		
		//Get the "attached" data from the previous activity
		Bundle data = getIntent().getExtras();
		
		//Get the list of exams from the previous activity and store it in a new arraylist
		ArrayList<Exam> examList = data.getParcelableArrayList(
				ResultActivity.this.getString(R.string.keyResultList));
		
		//Update the view with the list
		setListAdapter(new ExamAdapter(this, R.layout.result_row, examList ));
		
	}

	/**
	 * If the user has selected an exam, a new activity with all the details of the exam
	 * will be started
	 */
	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
	
		super.onListItemClick(l, v, position, id);
				//Cast the object into an exam object
				Exam o = (Exam) this.getListAdapter().getItem(position);
				Intent examIntent = new Intent(ResultActivity.this, ShowExamActivity.class);
				
				//Move the selected exam object to next activity
				examIntent.putExtra(
						ResultActivity.this.getString(R.string.keySelectedExam), o);
				//Menu options for delete favourite will be disabled
				examIntent.putExtra(
						ResultActivity.this.getString(R.string.keyMenuEnable), false);
				//Start the activivity
				ResultActivity.this.startActivity(examIntent); 
	}
	
	
}